<template>
      <div>
            <!-- 日期选择组件 -->
            <mdltimeComponent :dateObj="menuObj.date" v-bind="$attrs"></mdltimeComponent>
            <!-- <storeComponent 
                  :store="menuObj.store" 
                  :company="menuObj.company" 
                  :mast="menuObj.mast.storeObj"
                  :isfav="$attrs.isfav"
            /> -->
            <!-- 店铺组件 -->
            <mdlStoreComponent
               :store="menuObj.store"
               :isfav="$attrs.isfav"
            ></mdlStoreComponent>
            <!-- 商品组件 -->
            <mdlProdComponent></mdlProdComponent>
            
            <mdlCustomerComponent />
            <buyIntervalComponent>
                  
            </buyIntervalComponent>
            <demoComponent
                :demoMsg="demoMsg"
            ></demoComponent>
            <el-input class="from-control" style="width:232px;" v-model="demoMsg" />
            <div class="from-group" style="padding-right:30px;">
                  <div class="row" style="text-align:center">
                        <div class="col-lg-12">
                              <button type="button" class="btn btn-warning" style="width:100px;" @click="loadPage()">クリア</button>
                              <button type="button" class="btn btn-primary" style="width:100px; margin-left:10px;" @click="search">実行</button>
                        </div>
                  </div>
            </div>
      </div>
      
</template>
<script>
// import storeComponent from './menu_store.vue';
// import prodComponent from './menu_prod.vue';
import mdlCustomerComponent from './mdl_customer.vue';
//mdlink
import mdltimeComponent from './mdl_time.vue';
import mdlStoreComponent from './mdl_store.vue';
import mdlProdComponent from './mdl_prod.vue';
import buyIntervalComponent from './mdl_buyInterval.vue';
import demoComponent from './mdl_demo.vue';

export default {
      name:'menuComponent',
      components:{
            // storeComponent,
            // prodComponent,
            mdlCustomerComponent,
            mdltimeComponent,
            buyIntervalComponent,
            mdlStoreComponent,
            mdlProdComponent,
            demoComponent
      },
      data(){
            return {
                  storeTime:'',
                  menuObj:{
                        date:{
                              //期间类型：past/now/dateGoods
                              dateCondition:'now',
                              //时间类型： m/M/w/d
                              dateType:'M',
                              dateArray:[
                                    {
                                          dateType:'M',
                                          dateValue:'20190501~20190601',
                                          id:'dateButtonGroupA'
                                    },{
                                          dateType:'M',
                                          dateValue:'20190501~20190601',
                                          id:'dateButtonGroupB'
                                    },{
                                          dateType:'M',
                                          dateValue:'20190501~20190601',
                                          id:'dateButtonGroupC'
                                    }
                              ]
                        },
                        mast:{
                              storeObj:{
                                    type:'company',
                                    value:'0000'
                              },
                              goodsObj:{
                                    type:'company',
                                    value:'0000'
                              }
                        },
                        company:'',
                        goods:{
                              type:'stratum',
                              jan:{
                                    value:new Array(3).fill(new Array()),
                                    name:[]
                              },
                              prod:{
                                    value:new Array(3).fill(new Array()),
                                    name:['グループA','グループB','グループC']
                              }
                        },
                        store:{type:'normal',time:'',timeType:'M',value:[]},
                        customer:{},
                        customerProperty: {sex:'指定なし',ageStart:'0',ageEnd:'999'},
                        customerType:'all'
                  },
                  demoMsg:"147258",
                  canSearch:true
            };
      },
      watch:{
            'menuObj.date.dateType'(){
                  this.menuObj.store.timeType = this.menuObj.date.dateType;
            },
            storeTime(){
                  this.menuObj.store.time = this.storeTime;
            },
            'menuObj.company'(){
                  this.$emit('companyOfPage',this.menuObj.company);
            }
      },
      methods:{
            mastData(val){
                  this.menuObj.mast.storeObj = val.store;
                  this.menuObj.mast.goodsObj = val.goods;
            },
            loadPage(){
                  window.location.reload();
            },
            search(){
                  var param = this.getSearchParam();
                  if(this.canSearch){
                        this.$emit('search',param);
                  }
            },
            getSearchParam(){
                  this.canSearch = true;
                  var param = {
                        usercd:global.userCd,
                        guid:this.$utils.getGuid(),
                        kigyo:this.menuObj.company,
                        storeType:this.menuObj.store.type
                  };
                  if(this.menuObj.date.dateCondition === 'past' || this.menuObj.date.dateCondition === 'now' ){
                        param.kikan_type = 'now';
                  }else{
                        param.kikan_type = this.menuObj.date.dateCondition;
                  }
                  if(this.menuObj.date.dateType === 'm' || this.menuObj.date.dateType === 'M'){
                        param.kikan_flg = 'm';
                  }else{
                        param.kikan_flg = this.menuObj.date.dateType;
                  }
                  param.fiscalMonthFlg = this.menuObj.date.dateType === 'm'?'1':'0';
                  if(this.checkTimeValueRight() && this.checkStoreValueNotNull() && this.checkGoodsValueRight() && this.checkCustomergroupNotNull()){
                        //时间
                        param.kikan_A = this.menuObj.date.dateArray[0].dateValue;
                        if(this.menuObj.date.dateCondition === 'past'){
                              param.kikan_B = '';
                        }else{
                              param.kikan_B = this.menuObj.date.dateArray[1].dateValue;
                        }
                        param.kikan_C = this.menuObj.date.dateArray[2].dateValue;
                        //mast
                        param.suntorymst={};
                        param.suntorymst.tenpo_kaisou_mst = this.menuObj.mast.storeObj.type === 'company'?'tenpo_kaisou_kigyomst':'tenpo_kaisou_suntorymst';
                        param.suntorymst.shouhin_kaisou_mst = this.menuObj.mast.goodsObj.type === 'company'?'shouhin_kaisou_kigyomst':'shouhin_kaisou_suntorymst';
                        param.suntorymst.selected_tenpo = this.menuObj.mast.storeObj.value;
                        param.suntorymst.selected_shouhin = this.menuObj.mast.goodsObj.value;
                        //店铺
                        param.ten_module = this.menuObj.store.value;
                        //商品条件
                        this.dealGoodsParam(param);
                        //顾客条件
                        param.customer_type = this.menuObj.customerType;
                        if(param.customer_type === 'member'){
                              //会员别数据
                              if(this.menuObj.customerProperty.sex !== '指定なし'){
                                    param.sex = this.menuObj.customerProperty.sex;
                              }
                              param.age = this.menuObj.customerProperty.ageStart + '-' + this.menuObj.customerProperty.ageEnd;
                        }
                        if(param.customer_type === 'group'){
                              //顾客group
                              param.customer_group = this.menuObj.customer;
                        }
                  }else{
                        this.canSearch = false;
                  }
                  return param;
            },
            setMenu(param){
                  this.menuObj = param;
                  this.$set(this.menuObj,'date',param.date);
            },
            checkTimeValueRight(){
                  if(this.menuObj.date.dateCondition === 'now'){
                        //指定期间,对比期间不能大于期间条件
                        if(this.menuObj.date.dateArray[0].dateValue === this.menuObj.date.dateArray[1].dateValue){
                              global.msg_instance.showMsgNew({
                                    title : 'CROSSABC_001'
                              });
                              return false;
                        }
                  }
                  return true;
            },
            checkStoreValueNotNull(){
                 if(this.menuObj.store.value.length > 0){
                       return true;
                 }
                 global.msg_instance.showMsgNew({
                        title : 'COMMON_019'
                  });
                 return false;
            },
            checkGoodsValueRight(){
                  if(this.menuObj.goods.type === 'jan' &&
                        (this.menuObj.goods.jan.value[0] === undefined || this.menuObj.goods.jan.value[0].length <= 0 ||
                        this.menuObj.goods.jan.value[1] === undefined || this.menuObj.goods.jan.value[1].length <= 0)
                  ){
                        global.msg_instance.showMsgNew({
                              title : 'COMMON_020'
                        });
                        return false;
                  }else if(this.menuObj.goods.type === 'stratum' && (this.menuObj.goods.prod.value[0]===undefined || this.menuObj.goods.prod.value[0].length <= 0 ||
                        this.menuObj.goods.prod.value[1]===undefined || this.menuObj.goods.prod.value[1].length <= 0)
                  ){
                        global.msg_instance.showMsgNew({
                              title : 'COMMON_020'
                        });
                        return false;
                  }else{
                        return true;
                  }
            },
            checkCustomergroupNotNull(){
                  if(this.menuObj.customerType === 'group' && JSON.stringify(this.menuObj.customer) === '{}'){                        
                        global.msg_instance.showMsgNew({
                              title:"COMMON_006"
                        })
                        return false;
                  }
                  return true;
            },
            dealGoodsParam(param){
                  if(this.menuObj.goods.type === 'stratum'){
                        param.syohintype = 'kaisou';
                        param.syohin_module = [];
                        for (var index = 0; index < this.menuObj.goods.prod.value.length; index++) {
                              param.syohin_module.push(
                                    {'name':this.menuObj.goods.prod.name[index].substr(0,20),
                                    'syohin_module':this.menuObj.goods.prod.value[index]}
                              );
                        }
                  }else{
                        param.syohintype = 'tanpin';
                        param.syohin_jan_module = [];
                        for (var indexJan = 0; indexJan < this.menuObj.goods.jan.value.length; indexJan++) {
                              if(this.menuObj.goods.jan.value[indexJan].length > 0 ){
                                    param.syohin_jan_module.push(
                                          {'name':this.menuObj.goods.jan.name[indexJan].substr(0,20),
                                          'syohin_module':this.menuObj.goods.jan.value[indexJan]}
                                    );
                              }
                        }
                  }
            },
            chooseCustomerMth(param){
                  this.menuObj.customer = param;
            }
      },
      mounted(){
            this.storeTime = this.menuObj.date.dateArray[0].dateValue;
            var vm = this;
            window.$('#'+this.menuObj.date.dateArray[0].id).on('change',function(){
                  vm.storeTime = window.$('#'+vm.menuObj.date.dateArray[0].id).val();
            });
      }
};
</script>